﻿using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text;
using System.Web;

namespace PSAMMatrixCloud.Common.HttpParameterCommon
{
    public class HttpParameterSubmit
    {
        /// <summary>
        /// 生成要请求给接收方的参数数组
        /// </summary>
        /// <param name="sParaTemp">请求前的参数数组</param>
        /// <param name="signKey">签名秘钥</param>
        /// <returns>要请求的参数数组</returns>
        public static Dictionary<string, string> BuildRequestPara(SortedDictionary<string, string> sParaTemp, string signKey)
        {
            //待签名请求参数数组
            Dictionary<string, string> sPara = new Dictionary<string, string>();
            //签名结果
            string mysign = "";

            //过滤签名参数数组
            sPara = HttpParameterCore.FilterPara(sParaTemp);

            //获得签名结果
            mysign = HttpParameterCore.BuildMysign(sPara, signKey);

            //签名结果与签名方式加入请求提交参数组中
            sPara.Add("sign", mysign);

            return sPara;
        }

        /// <summary>
        /// 生成要请求给接收方的参数数组
        /// </summary>
        /// <param name="sParaTemp">请求前的参数数组</param>
        /// <param name="signKey">签名秘钥</param>
        /// <returns>要请求的参数数组字符串</returns>
        public static string BuildRequestParaToString(SortedDictionary<string, string> sParaTemp, string signKey)
        {
            //待签名请求参数数组
            var sPara = BuildRequestPara(sParaTemp, signKey);

            //发送请求前需要把参数值进行UrlEncode
            var requestPara = new Dictionary<string, string>();
            foreach (var item in sPara)
            {
                requestPara[item.Key] = HttpUtility.UrlEncode(item.Value);
            }

            //把参数组中所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
            string strRequestData = HttpParameterCore.CreateLinkString(requestPara);

            return strRequestData;
        }

        /// <summary>
        /// 构造模拟远程HTTP的GET请求，获取接收方返回的Json处理结果
        /// </summary>
        /// <param name="sParaTemp">请求参数数组</param>
        /// <param name="gateway">网关地址</param>
        /// <param name="signKey">签名秘钥</param>
        /// <returns>接收方返回的Json处理结果</returns>
        public static string SendGetInfo(SortedDictionary<string, string> sParaTemp, string gateway, string signKey)
        {
            //待请求参数数组字符串
            string strRequestData = BuildRequestParaToString(sParaTemp, signKey);

            //构造请求地址
            string strUrl = gateway + "?" + strRequestData;

            //请求远程HTTP
            string strResult = "";

            //设置HttpWebRequest基本信息
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(strUrl);
            myReq.Method = "GET";
            myReq.Timeout = 10000;

            //发送Get数据请求服务器
            using (HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse())
            {
                using (StreamReader srReader = new StreamReader(HttpWResp.GetResponseStream(), Encoding.UTF8))
                {
                    strResult = srReader.ReadToEnd();//获取服务器返回信息
                }
            }

            return strResult;
        }

        /// <summary>
        /// 建立请求，以表单HTML形式构造（默认）
        /// </summary>
        /// <param name="sParaTemp">请求参数数组</param>
        /// <param name="strMethod">提交方式。两个值可选：post、get</param>
        /// <param name="strButtonValue">确认按钮显示文字</param>
        /// <returns>提交表单HTML文本</returns>
        public static string BuildRequest(string gateway, SortedDictionary<string, string> sParaTemp, string strMethod, string strButtonValue, string signKey)
        {
            //待请求参数数组
            Dictionary<string, string> dicPara = new Dictionary<string, string>();
            dicPara = Common.HttpParameterCommon.HttpParameterSubmit.BuildRequestPara(sParaTemp, signKey);

            StringBuilder sbHtml = new StringBuilder();

            sbHtml.Append("<form id='pmspaysubmit' name='pmspaysubmit' action='" + gateway + "_input_charset=utf-8' method='" + strMethod.ToLower().Trim() + "'>");

            foreach (KeyValuePair<string, string> temp in dicPara)
            {
                sbHtml.Append("<input type='hidden' name='" + temp.Key + "' value='" + temp.Value + "'/>");
            }

            //submit按钮控件请不要含有name属性
            sbHtml.Append("<input type='submit' value='" + strButtonValue + "' style='display:none;'></form>");

            sbHtml.Append("<script>document.forms['pmspaysubmit'].submit();</script>");

            return sbHtml.ToString();
        }
    }
}